Emergency Services Project - Raspberry Pi Pico

This is an experiment with Chat GPT - may not work correctly!

In this project, you'll create emergency lights and sounds, just like the ones used by police cars, ambulances, and fire trucks in the UK!

What You Will Need

Step 1: Install Thonny

On Windows:

On A CoderDojo Laptop (running Raspberry Pi OS):

Step 2: Set Up Raspberry Pi Pico in Thonny

Step 3: Build the Circuit

Let’s build the circuit step by step. If you’ve never connected an LED before, don’t worry! Follow these instructions carefully.

Connecting the LED

An LED has two legs. The longer leg is called the positive leg, or the anode, and it connects to the power (in our case, a GPIO pin on the Raspberry Pi Pico). The shorter leg is called the negative leg, or the cathode, and it needs to connect to GND (ground).

Here’s how to connect the LED:

Connecting the Buzzer

The buzzer will create the sound of a siren. It also has a positive and negative side. Usually, the positive side is marked with a plus sign (+). Here’s how to connect it:

Connecting the Button

Finally, let’s add a button to control when the lights and sounds are activated. The button will act as a switch, letting us start the lights and siren by pressing it. Here’s how to connect it:

Step 4: Writing the Code

Let’s start by writing some code in Thonny to control the LED. We’re going to add a little bit of code at a time, so that you can understand how each part works. Don’t worry, we’ll explain everything!

What Are Comments?

Comments are lines in your code that the computer ignores. We use comments to explain what the code does, which is really helpful for you (and others) when reading through your program later.

Comments in Python start with the # symbol. Anything on the same line after this symbol is ignored by the computer. For example:

# This is a comment, the computer skips this line

Step 4.1: Setting Up the LED

Let’s write the code to set up the LED. Type this into Thonny:

from machine import Pin  # Import Pin to control the GPIO pins

# Set up the LED on GP15 as an output
led = Pin(15, Pin.OUT)

Explanation:

Step 4.2: Making the LED Blink

Now, let’s add a function to make the LED blink on and off. Type this into Thonny:

from time import sleep  # Import the sleep function to create delays

# Function to make the LED blink
def blink_led():
    led.on()  # Turn the LED on
    sleep(0.5)  # Wait for half a second
    led.off()  # Turn the LED off
    sleep(0.5)  # Wait for half a second

Explanation:

Step 4.3: Blinking the LED in a Loop

We want the LED to blink continuously, not just once. So, let’s add a loop to repeat the blinking. Type this into Thonny:

# Main loop to keep the LED blinking
while True:
    blink_led()  # Call the blink_led function to make the LED blink

Explanation:

Step 4.4: Controlling the Buzzer

Now let’s add code to control the buzzer and make a siren sound. Add this to your code:

# Set up the buzzer on GP14 as an output
buzzer = Pin(14, Pin.OUT)

# Function to make the buzzer sound
def sound_siren():
    for i in range(5):  # Repeat 5 times
        buzzer.on()  # Turn the buzzer on
        sleep(0.2)  # Wait for 0.2 seconds
        buzzer.off()  # Turn the buzzer off
        sleep(0.2)  # Wait for 0.2 seconds

Explanation:

Final Code

Here’s what your final code should look like:

from machine import Pin
from time import sleep

# Set up the LED and buzzer
led = Pin(15, Pin.OUT)
buzzer = Pin(14, Pin.OUT)

# Function to make the LED blink
def blink_led():
    led.on()
    sleep(0.5)
    led.off()
    sleep(0.5)

# Function to make the buzzer sound
def sound_siren():
    for i in range(5):
        buzzer.on()
        sleep(0.2)
        buzzer.off()
        sleep(0.2)

# Main loop to run both the LED and the buzzer
while True:
    blink_led()
    sound_siren()